clear all
set more off 

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"
quiet do load_rreg2


cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear



*** winsorize/drop unrealistic observations
winsor2 edy_12m, cuts(2 98) suffix(_w)
winsor2 edy_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen edy_12m_w_X_IT=edy_12m_w*treatment_IT
gen edy_12m_w_X_AIT=edy_12m_w*treatment_AIT

*** winsorize/drop unrealistic observations
winsor2 egdp_12m, cuts(2 98) suffix(_w)
winsor2 egdp_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen egdp_12m_w_X_IT=egdp_12m_w*treatment_IT
gen egdp_12m_w_X_AIT=egdp_12m_w*treatment_AIT

*** probability that inflation is greater than 4 percent (prior)
gen prob_prior=	epi_12m_12plus + epi_12m_8_12 + epi_12m_4_8


*** winsorize/drop unrealistic observations
winsor2 epi_12m, cuts(2 98) suffix(_w)
winsor2 epi_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen epi_12m_w_X_IT=epi_12m_w*treatment_IT
gen epi_12m_w_X_AIT=epi_12m_w*treatment_AIT

gen epi_12m_mean_X_IT=epi_12m_mean*treatment_IT
gen epi_12m_mean_X_AIT=epi_12m_mean*treatment_AIT

*** define groups

gen GR1=1 if Q48==1 | Q48==2 /* Less than high school OR igh school diploma */
gen GR2=1 if Q48==3 /* Some college, but no degree */
gen GR3=1 if Q48==4 /*  Bachelor's degree */
gen GR4=1 if Q48==5 | Q48==6 /*  Master's degree OR Doctorate or Professional Degree */

gen GR5=1 if Q3==1 /*female */
gen GR6=1 if Q3==2 /*male */

gen GR7=1 if Q49==0 | Q49==1 /* less than 19K */
gen GR8=1 if Q49==2 | Q49==3 /* 20K-50K */
gen GR9=1 if  Q49==4 /* 50K-100K */
gen GR10=1 if  Q49==5 | Q49==6 /* more than 100K */

gen GR11=1 if party_democrat==1 
gen GR12=1 if party_republi==1 
gen GR13=1 if party_democrat==0 &  party_republi==0

gen GR14=1 if Q27==1 /* save around 10% of my monthly income */
gen GR15=1 if Q27==2  | Q27==3 /* save around + 25% of my monthly income */
gen GR16=1 if Q27==4  | Q27==5 /* I spend all of my income each OR spend more than earn */

 
*** compute the difference non-parametrically

	
tempname 2
postfile `2'  ///
	str20 outcome ///
	str10 b_treatment_IT ///
	str10 b_treatment_AIT ///
	str10 pval ///		
	N_reg ///
	str10 R2_reg ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Appendix_Table10_Hubers.dta", replace every(1)
	
*===============================================================================
*			regressions forvariables with priors
*===============================================================================

foreach var in epi_12m_w   {

* foreach var in epi_12m_w prob_prior_w egdp_12m_w edy_12m_w {
	
	capture drop POSTERIOR
	if "`var'"=="epi_12m_w" gen POSTERIOR=epi_5y_w

	
	capture drop wgtXb2
	
	capture drop _*
	gen _POSTERIOR=POSTERIOR*sqrt(weight)
	gen _treatment_IT=treatment_IT*sqrt(weight)
	gen _treatment_AIT=treatment_AIT*sqrt(weight)
	gen _W=sqrt(weight)
	
	rreg2 _POSTERIOR _treatment_IT _treatment_AIT _W, nolog gen(wgtXb2) 
	replace wgtXb2=wgtXb2*weight
	
	
	*** HUBER
	reg POSTERIOR treatment_IT treatment_AIT [aw=wgtXb2], robust
	
	foreach vv in treatment_IT treatment_AIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval=string(r(p),"%4.3f")
	
	*** full sample
	post `2' ("`var'")   ("`b_treatment_IT'") ("`b_treatment_AIT'")  ("`pval'")  (e(N))  (string(e(r2),"%4.3f") )
	post `2' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") (" ")   (.) (" ")	
	
	*** iterate across demographic groups
	forvalues ix=1(1)16 {
			reg POSTERIOR treatment_IT treatment_AIT [aw=wgtXb2] if GR`ix'==1, robust
			
			foreach vv in treatment_IT treatment_AIT {
				local b_`vv'=string(_b[`vv'],"%4.3f")
				local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
				
				local p1=_b[`vv']/_se[`vv']
				
				if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
				if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
				if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
			}
	
			test  treatment_IT = treatment_AIT
			local pval=string(r(p),"%4.3f")
	
			post `2' ("`var' + GR `ix'")   ("`b_treatment_IT'") ("`b_treatment_AIT'")  ("`pval'")  (e(N))  (string(e(r2),"%4.3f") )
			post `2' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") (" ")   (.) (" ")	
				
	}
}

postclose `2'

